package com.ymx.darling.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 爱Java的小于
 */
public class ArrayUtil {
    @SafeVarargs
    public static <T> List<T> asList(T... values){
        return new ArrayList<>(Arrays.asList(values));
    }


    /**
     * 判断是否是数组类型
     * @param object obj
     * @return boolean
     */
    public static boolean isArray(Object object) {
        return object != null && object.getClass().isArray();
    }

    /**
     * 判断是否是数组类型
     * @param type type
     * @return boolean
     */
    public static boolean isArray(Class<?> type) {
        return type != null && type.isArray();
    }


    public static String toString(Object obj) {
        if (obj == null) {
            return "null";
        } else {
            Class<?> clazz = obj.getClass();
            if (!clazz.isArray()) {
                return obj.toString();
            } else {
                Class<?> componentType = obj.getClass().getComponentType();
                if (Long.TYPE.equals(componentType)) {
                    return Arrays.toString((long[])obj);
                } else if (Integer.TYPE.equals(componentType)) {
                    return Arrays.toString((int[])obj);
                } else if (Short.TYPE.equals(componentType)) {
                    return Arrays.toString((short[])obj);
                } else if (Character.TYPE.equals(componentType)) {
                    return Arrays.toString((char[])obj);
                } else if (Byte.TYPE.equals(componentType)) {
                    return Arrays.toString((byte[])obj);
                } else if (Boolean.TYPE.equals(componentType)) {
                    return Arrays.toString((boolean[])obj);
                } else if (Float.TYPE.equals(componentType)) {
                    return Arrays.toString((float[])obj);
                } else {
                    return Double.TYPE.equals(componentType) ? Arrays.toString((double[])obj) : Arrays.toString((Object[])obj);
                }
            }
        }
    }
}
